#include<iostream>
#include<cstdio>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<cstring>
#include<cstdlib>
#include<errno.h>
#include<fcntl.h>
#include<cassert>


#define PATHNAME "/home/ldh/tmp/mypipe"


int main()
{
    //创建一个命名管道文件
    umask(0000);
    int ret=mkfifo(PATHNAME,0777);
    if(ret==-1)
    {
        std::cout<<strerror(errno)<<std::endl;
        exit(-1);
    }
    std::cout<<"Creat pipe sucess !"<<std::endl;
    (void)ret;
    //假设sever用于接收数据
    int fd=open(PATHNAME,O_RDONLY);
    assert(fd!=-1); 
    std::cout<<"open pipe success!"<<std::endl;
    char buffer[1024];
    while(1)
    {
        int rd_ret=read(fd,buffer,sizeof buffer -1);
        if(rd_ret==0)
            break;
        buffer[strlen(buffer)]='\0';
        std::cout<<buffer<<std::endl;
    }
    close(fd);
    sleep(1); //休眠一下确保两个进程都关闭文件
    unlink(PATHNAME);

    return 0;
}
